using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.Office.Server;
using Microsoft.Office.Server.UserProfiles;

using Behemoth.Office.Server;

using Yammer;

namespace Behemoth.SharePoint.Web.YammerAPI {

  public class YammerPropertyCreator : ProfilePropertyCreator {

    public YammerPropertyCreator(SPSite site) : base(site) { }
    public YammerPropertyCreator() : base() { }

    protected override string[] AllowedPropertyNames {
      get { return new string[] { "YammerSettings" }; }
    }

    public bool SaveSettings(Settings settings) {
      if (settings == null)
        return false;
      byte[] arr = settings.ToByteArray();
      MyPropertySaveEventArgs args = new MyPropertySaveEventArgs("YammerSettings", arr);
      this.SaveProperties_Elevated(args);
      return true;
    }
    public Settings LoadSettings() {
      UserProfile profile = GetProfile();
      if (profile["YammerSettings"] == null || profile["YammerSettings"].Value == null)
        return null;
      byte[] arr = (byte[])profile["YammerSettings"].Value;
      Settings settings = Settings.CreateFromByteArray(arr);
      return settings;
    }

    protected override void CreateProperties(UserProfileConfigManager profileConfigManager) {
      PropertyCollection propertyCollection = profileConfigManager.GetProperties();
      PropertyDataType ptype = GetPropertyType(profileConfigManager, "foo");
      // NOTE comment these normally..its to blow away stuff in development
      /*
      if (PropertyExists("YammerSettings")) {
        propertyCollection.RemoveByName("YammerSettings", false);
      } */
      // END NOTE
      if (!PropertyExists("YammerSettings")) {
        Property property = propertyCollection.Create(false);
        property.Name = "YammerSettings";
        property.DisplayName = "Yammer Settings Object";
        property.Type = "binary";
        property.PrivacyPolicy = PrivacyPolicy.OptIn;
        property.DefaultPrivacy = Privacy.Private;
        property.Description = "Yammer authentication settings object.";
        property.IsSearchable = false;
        property.IsVisibleOnEditor = false;
        property.IsAlias = false;
        property.Length = 7500;
        property.IsUserEditable = true; // we would prefer this be admin only, but it threw an error when we tried to update it via code
        propertyCollection.Add(property);
      }
    }
  
  } // class
} // namespace
